package com.lzk.jetpacktest.rxhttp

import com.lzk.jetpacktest.BuildConfig
import okhttp3.Cache
import okhttp3.CookieJar
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import rxhttp.RxHttpPlugins
import rxhttp.wrapper.ssl.HttpsUtils
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * @Author: LiaoZhongKai
 * @Date: 2021/9/29 10:10
 * @Description:
 */
object RxHttpManager {
    private val sslParams = HttpsUtils.getSslSocketFactory()

    private val mOkHttpClient: OkHttpClient = OkHttpClient.Builder()
        .connectTimeout(100, TimeUnit.SECONDS)//与服务器建立连接的时长，默认10s
        .readTimeout(100, TimeUnit.SECONDS)//读取服务器返回数据的时长
        .writeTimeout(100, TimeUnit.SECONDS)//向服务器写入数据的时长，默认10s
        .sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
        .hostnameVerifier { _, _ -> true }
        .cache(Cache(File("sdcard/cache"), 1024))//http 的缓存大小，位置
        .cookieJar(CookieJar.NO_COOKIES)//不使用cookie，若用，自定义cookieJar实现
        .addNetworkInterceptor(HttpLoggingInterceptor())
        .build()

    fun init(){
        RxHttpPlugins.init(mOkHttpClient)
            .setDebug(BuildConfig.DEBUG)
    }
}